package com.hzw.service;

import com.hzw.api.IOrderService;
import com.hzw.common.DateUtils;
import com.hzw.dao.OrderCommodityDao;
import com.hzw.dao.OrderDao;
import com.hzw.dao.ShoppingCarDao;
import com.hzw.dao.UserDao;
import com.hzw.entity.Order;
import com.hzw.entity.OrderCommodity;
import com.hzw.entity.ShoppingCar;
import com.hzw.entity.User;
import com.hzw.entity.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class OrderService implements IOrderService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    OrderCommodityDao orderCommodityDao;

    @Autowired
    UserDao userDao;

    @Autowired
    ShoppingCarDao shoppingCarDao;

    @Override
    public List<Map<String, Object>> select(Order order) {
        List<Map<String, Object>> resMapList = new ArrayList<>();
        List<Order> orderList = orderDao.select(order);
        if (CollectionUtils.isEmpty(orderList)) {
            return resMapList;
        }
        for (Order o : orderList) {
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("id", o.getId());
            resMap.put("createTime", o.getCreateTime());
            resMap.put("finishTime", o.getFinishTime());
            resMap.put("status", o.getStatus());
            //查询订单下的商品信息
            OrderCommodity orderCommodity = new OrderCommodity();
            orderCommodity.setOrderId(o.getId());
            List<OrderCommodity> orderCommodityList = orderCommodityDao.select(orderCommodity);
            resMap.put("orderCommodityList", orderCommodityList);
            //查询当前订单下下用户信息
            User user = new User();
            user.setUsername(o.getUserId());
            List<User> userList = userDao.select(user);
            User resUser = userList.get(0);
            resMap.put("relName", resUser.getRealName());
            resMap.put("phone", resUser.getPhone());
            resMap.put("userName", resUser.getUsername());
            resMap.put("place", resUser.getAddress());
            resMapList.add(resMap);
        }
        return resMapList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(OrderVo orderVo) {
        String orderId = UUID.randomUUID().toString().replace("-", "");
        Order order = new Order();
        order.setId(orderId);
        order.setStatus(0);
        order.setUserId(orderVo.getUserId());
        order.setCreateTime(DateUtils.getNowTime());
        order.setPhone(orderVo.getPhone());
        order.setRealName(orderVo.getRealName());
        order.setPlace(orderVo.getPlace());
        order.setPrice(orderVo.getPrice());
        //循环插入订单内商品信息
        List<OrderCommodity> orderCommodityList = orderVo.getOrderCommodityList();
        for (OrderCommodity orderCommodity : orderCommodityList) {
            orderCommodity.setId(UUID.randomUUID().toString().replace("-", ""));
            orderCommodity.setOrderId(orderId);

            orderCommodityDao.insert(orderCommodity);
        }
        int i = orderDao.insert(order);
        ShoppingCar shoppingCar = new ShoppingCar();
        shoppingCar.setUserId(orderVo.getUserId());
        shoppingCarDao.delByUserId(shoppingCar);
        return i;
    }

    @Override
    public void delete(Order order) {
        OrderCommodity orderCommodity = new OrderCommodity();
        orderCommodity.setOrderId(order.getId());
        orderCommodityDao.delByOrderId(orderCommodity);
        orderDao.delByUserId(order);
    }

    @Override
    public void update(Order order) {
        if (!StringUtils.isEmpty(order.getStatus()) && order.getStatus() == 2) {
            order.setFinishTime(DateUtils.getNowTime());
        }
        orderDao.updateByUserId(order);
    }
}
